/**
 * @file   Float64.cpp
 * @author Djuro Drljaca (djurodrljaca@gmail.com)
 * @date   2014-04-19
 * @brief  DON data type object: Float64
 *
 * Copyright 2014  Djuro Drljaca (djurodrljaca@gmail.com)
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <DON/Core/DataType/Float64.h>
#include <DON/Core/DataType/UInt64.h>
#include <DON/Core/Platform/PlatformDefines.h>
#include <QtCore/QByteArray>

using namespace DON::Core::DataType;

class Float64::Private
{
public:
    Private()
        : value(0.0)
    { }

    double value;
};

Float64::Float64()
    : AbstractDataType(),
      d(new Private())
{
}

Float64::Float64(const Float64 &other)
    : AbstractDataType(),
      d(new Private())
{
    d->value = other.d->value;
}

Float64::~Float64()
{
    delete d;
}

Float64 & Float64::operator=(const Float64 &other)
{
    d->value = other.d->value;

    return *this;
}

AbstractDataType * Float64::clone() const
{
    return new Float64(*this);
}

DataType Float64::getDataType() const
{
    return DataType_Float64;
}

double Float64::getValue() const
{
    return d->value;
}

void Float64::setValue(const double value)
{
    d->value = value;
}

bool Float64::fromBinary(const QByteArray &data, const int startIndex, int *size)
{
    bool success = false;
    double value = Float64::fromBinary(data, startIndex, &success);

    if (success)
    {
        d->value = value;

        if (size != NULL)
        {
            *size = 8;
        }
    }

    return success;
}

QByteArray Float64::toBinary() const
{
    return Float64::toBinary(d->value);
}

Float64 * Float64::fromAbstractDataType(AbstractDataType *instance)
{
    Float64 *outputInstance = NULL;

    if (instance != NULL)
    {
        if (instance->getDataType() == DataType_Float64)
        {
            outputInstance = static_cast<Float64 *>(instance);
        }
    }

    return outputInstance;
}

double Float64::fromBinary(const QByteArray &data, const int startIndex, bool *ok)
{
    bool success = false;
    double value = 0.0;

#if (DON_CORE_PLATFORM_ENDIANESS_FLOAT64 == DON_CORE_PLATFORM_LITTLE_ENDIAN) || \
    (DON_CORE_PLATFORM_ENDIANESS_FLOAT64 == DON_CORE_PLATFORM_BIG_ENDIAN)
    union
    {
        quint64 valueUInt64;
        double valueFloat64;
    } converter;


    converter.valueUInt64 = UInt64::fromBinary(data, startIndex, &success);

    if (success)
    {
        value = converter.valueFloat64;
    }
#else
#  error "Unknown FLOAT64 endianess!"
#endif

    if (ok != NULL)
    {
        *ok = success;
    }

    return value;
}

QByteArray Float64::toBinary(const double value)
{
    quint64 rawValue = 0ull;

#if (DON_CORE_PLATFORM_ENDIANESS_FLOAT64 == DON_CORE_PLATFORM_LITTLE_ENDIAN) || \
    (DON_CORE_PLATFORM_ENDIANESS_FLOAT64 == DON_CORE_PLATFORM_BIG_ENDIAN)
    union
    {
        quint64 valueUInt64;
        double valueFloat64;
    } converter;

    converter.valueFloat64 = value;
    rawValue = converter.valueUInt64;
#else
#  error "Unknown FLOAT64 endianess!"
#endif

    return UInt64::toBinary(rawValue);
}
